package com.cpp.server.controller;

import com.cpp.common.constant.JwtClaimsConstant;
import com.cpp.common.constant.ModuleConstant;
import com.cpp.common.enums.UserRoleEnum;
import com.cpp.common.properties.JwtProperties;
import com.cpp.common.result.PageResult;
import com.cpp.common.result.Result;
import com.cpp.common.utils.JwtUtil;
import com.cpp.pojo.dto.user.UserCreateDTO;
import com.cpp.pojo.dto.user.UserLoginDTO;
import com.cpp.pojo.dto.user.UserPageDTO;
import com.cpp.pojo.dto.user.UserUpdateDTO;
import com.cpp.pojo.pojo.User;
import com.cpp.pojo.vo.user.UserLoginVO;
import com.cpp.server.aspect.annotation.IgnoreLog;
import com.cpp.server.aspect.annotation.MyLog;
import com.cpp.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/user")
@MyLog(module = ModuleConstant.USER)
public class UserController {
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @IgnoreLog
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
        //登录校验
        User user = userService.login(userLoginDTO);

        //生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.ID, user.getId());
        claims.put(JwtClaimsConstant.USERNAME, user.getUsername());
        claims.put(JwtClaimsConstant.REAL_NAME, user.getRealName());
        claims.put(JwtClaimsConstant.SEX, user.getSex());
        claims.put(JwtClaimsConstant.IMAGE_URL, user.getImageUrl());
        claims.put(JwtClaimsConstant.ROLE, user.getRole());
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);

        // 封装数据
        UserLoginVO userLoginVO = UserLoginVO
                .builder()
                .username(user.getUsername())
                .sex(user.getSex().equals("1") ?"男":"女")
                .imageUrl(user.getImageUrl())
                .role(user.getRole())
                .roleName(UserRoleEnum.getType(user.getRole()).getDesc())
                .realName(user.getRealName())
                .token(token)
                .build();

        return Result.success(userLoginVO);
    }
    //管理员新增用户
    @PostMapping("/save")
    public Result save(@RequestBody UserCreateDTO userCreateDTO){
        userService.saveUser(userCreateDTO);
        return Result.success();
    }

    //管理员查看用户列表
    @GetMapping("/list")
    public Result<PageResult> listUser(UserPageDTO userPageDTO){
        PageResult pageResult = userService.list(userPageDTO);
        return Result.success(pageResult);
    }

    //管理员删除用户
    @DeleteMapping("/delete/{ids}")
    public Result deleteUser(@PathVariable List<Long> ids){
        userService.removeByIds(ids);
        return Result.success();
    }

    //管理员修改用户信息
    @PutMapping("/update")
    public Result updateUser(@RequestBody UserUpdateDTO userUpdateDTO){
        userService.updateUser(userUpdateDTO);
        return Result.success();
    }

}
